package com.lyl.xxx.modules.system.account.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;
import com.lyl.xxx.modules.system.account.dao.SysRoleDao;
import com.lyl.xxx.modules.system.account.entity.SysRole;
import com.lyl.xxx.modules.system.account.service.SysRoleService;
import com.lyl.xxx.modules.web.account.model.dto.RoleDTO;
import com.lyl.xxx.modules.web.account.model.vo.RoleVO;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户角色表(SysRole)表服务实现类
 *
 * @author Mr Luo
 * @since 2022-09-19 11:14:04
 */
@Service("sysRoleService")
public class SysRoleServiceImpl extends ServiceImpl<SysRoleDao, SysRole> implements SysRoleService {

    @Override
    public PageInfo<RoleVO> list(Integer pageNo, Integer pageSize, RoleDTO dto) {
        PageMethod.startPage(pageNo,pageSize);
        List<SysRole> roleList = lambdaQuery()
                .like(CharSequenceUtil.isNotBlank(dto.getRoleName()), SysRole::getRoleName, dto.getRoleName())
                .like(CharSequenceUtil.isNotBlank(dto.getRoleKey()), SysRole::getRoleKey, dto.getRoleKey())
                .eq(ObjectUtil.isNotEmpty(dto.getStatus()), SysRole::getStatus, dto.getStatus())
                .ge(CharSequenceUtil.isNotBlank(dto.getStartTime()), SysRole::getCreateTime, dto.getStartTime())
                .le(CharSequenceUtil.isNotBlank(dto.getEndTime()), SysRole::getCreateTime, dto.getEndTime())
                .orderByAsc(SysRole::getOrderNum)
                .list();
        PageInfo<SysRole> rolePageInfo = PageInfo.of(roleList);
        // 类型转换
        List<RoleVO> voList = roleList.stream()
                .map(sysRole -> BeanUtil.copyProperties(sysRole, RoleVO.class))
                .collect(Collectors.toList());
        PageInfo<RoleVO> voPageInfo = PageInfo.of(voList);
        BeanUtil.copyProperties(rolePageInfo,voPageInfo,"list");
        return voPageInfo;
    }
}

